package crayon.fc;

import java.awt.Point;
import java.util.ArrayList;


public class Gestionnaire_Point{
    public ArrayList<SpecializedPoint> m_Plist;
    public SpecializedPoint closePoint1,closePoint2;
    public static final int  CubeDemiSize = 5;
    
    public Gestionnaire_Point(){
	m_Plist= new ArrayList<SpecializedPoint>();
	closePoint1=null;
	closePoint2=null;
		
    }
    
    public boolean InMyList(Point p){
	for (SpecializedPoint itr: m_Plist)
	    if (itr.point.x<=p.x+CubeDemiSize && itr.point.x>=p.x-CubeDemiSize && itr.point.y<=p.y+CubeDemiSize && itr.point.y>=p.y-CubeDemiSize )
		return true;
	return false;
    }
    public boolean InMyListTan(Point p){
	for (SpecializedPoint itr: m_Plist){
	    if(itr.ctrl1!=null){
		if (itr.ctrl1.x<=p.x+CubeDemiSize && itr.ctrl1.x>=p.x-CubeDemiSize && itr.ctrl1.y<=p.y+CubeDemiSize && itr.ctrl1.y>=p.y-CubeDemiSize )
		    return true;
	    }
	    if(itr.ctrl2!=null){
		if (itr.ctrl2.x<=p.x+CubeDemiSize && itr.ctrl2.x>=p.x-CubeDemiSize && itr.ctrl2.y<=p.y+CubeDemiSize && itr.ctrl2.y>=p.y-CubeDemiSize )
		    return true;
	    }
	}
	return false;
    }
    
    
    public Point getPoint(Point p){
	for (SpecializedPoint itr: m_Plist)
	    if (itr.point.x<=p.x+CubeDemiSize && itr.point.x>=p.x-CubeDemiSize && 
		itr.point.y<=p.y+CubeDemiSize && itr.point.y>=p.y-CubeDemiSize )
		return itr.point;
	return null;
    }
    public Point getPointTan(Point p){
	for (SpecializedPoint itr: m_Plist){
	    if(itr.ctrl1!=null){
		if (itr.ctrl1.x<=p.x+CubeDemiSize && itr.ctrl1.x>=p.x-CubeDemiSize && itr.ctrl1.y<=p.y+CubeDemiSize && itr.ctrl1.y>=p.y-CubeDemiSize )
		    return itr.point;
	    }
	    if(itr.ctrl2!=null){
		if (itr.ctrl2.x<=p.x+CubeDemiSize && itr.ctrl2.x>=p.x-CubeDemiSize && itr.ctrl2.y<=p.y+CubeDemiSize && itr.ctrl2.y>=p.y-CubeDemiSize )
		    return itr.point;
	    }

	}
	return null;
    }
    
    public void add(Point p ){
	m_Plist.add(new SpecializedPoint(p));
    }
    
    public void set(Point p,Point newPoint ){
	int index=-1, i=0;
	for (SpecializedPoint itr: m_Plist){
	    if(itr.point==p){
		index=i;
	    }
	    else 
		i++;
	}
	if(index!=-1){
	    ((SpecializedPoint)m_Plist.get(index)).point=newPoint;
	    if( ((SpecializedPoint)m_Plist.get(index)).ctrl1!=null){
		((SpecializedPoint)m_Plist.get(index)).ctrl1=new Point(
		    ((SpecializedPoint)m_Plist.get(index)).ctrl1.x+(newPoint.x-p.x),		    
		    ((SpecializedPoint)m_Plist.get(index)).ctrl1.y+(newPoint.y-p.y));
		    }
	    if( ((SpecializedPoint)m_Plist.get(index)).ctrl2!=null)
		 ((SpecializedPoint)m_Plist.get(index)).ctrl2=new
		     Point(newPoint.x-(((SpecializedPoint)m_Plist.get(index)).ctrl1.x-newPoint.x),
			   newPoint.y-(((SpecializedPoint)m_Plist.get(index)).ctrl1.y-newPoint.y));	    
	    
	}
    }
    
    public void setCtrl(Point p,Point newPoint ){
	int index=-1, i=0;
	for (SpecializedPoint itr: m_Plist){
	    if(itr.point==p){
		index=i;
	    }
	    else 
		i++;
	}
	if(index!=-1){
	    ((SpecializedPoint)m_Plist.get(index)).ctrl1=newPoint;
	    ((SpecializedPoint)m_Plist.get(index)).ctrl2=new Point(p.x-(newPoint.x-p.x),p.y-(newPoint.y-p.y));
	}
    }
    
    public void setClosePoint(Point p1, Point p2){
	for (SpecializedPoint itr: m_Plist)
	    if (itr.point.x<=p2.x+CubeDemiSize && itr.point.x>=p2.x-CubeDemiSize && itr.point.y<=p2.y+CubeDemiSize && itr.point.y>=p2.y-CubeDemiSize ){
		closePoint1=itr;
	    }
	for (SpecializedPoint itr: m_Plist)
	    if (itr.point.x<=p1.x+CubeDemiSize && itr.point.x>=p1.x-CubeDemiSize && itr.point.y<=p1.y+CubeDemiSize && itr.point.y>=p1.y-CubeDemiSize ){
		closePoint2=itr;
				
	    }

  }
    public Point get(int index){
	return m_Plist.get(index).point;
    }
    public void delete(Point p){
		int index=-1, i=0;
	for (SpecializedPoint itr: m_Plist){
	    if(itr.point==p){
		index=i;
	    }
	    else 
		i++;
	}
	if(index!=-1){
	    if(closePoint1==((SpecializedPoint)m_Plist.get(index)))
		closePoint1=null;
	    
	    if(closePoint2==((SpecializedPoint)m_Plist.get(index)))
		closePoint2=null;
	    m_Plist.remove(index);
	}
    }
}